﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using ShakeGestures;
using System.Windows.Data;
using System.Globalization;

//Application ID: 21c52bcd-0d22-4059-8c9f-e59dc0de0361
//Ad Unit ID: 10036062


namespace RulesOfAcquisition
{
	public partial class MainPage : PhoneApplicationPage
	{
		// Constructor
		public MainPage()
		{
			InitializeComponent();

			// Set the data context of the listbox control to the sample data
			DataContext = App.ViewModel;
			this.Loaded += new RoutedEventHandler(MainPage_Loaded);

			// register shake event
			ShakeGesturesHelper.Instance.ShakeGesture += new
				EventHandler<ShakeGestureEventArgs>(Instance_ShakeGesture);

			// optional, set parameters
			ShakeGesturesHelper.Instance.MinimumRequiredMovesForShake = 4;
			
			// Assume starts on random tab
			ShakeGesturesHelper.Instance.Active = true;
		}

		private void Instance_ShakeGesture(object sender, ShakeGestureEventArgs e)
		{
			// This event handler isn't on the UI thread, but we need to execute there
			Dispatcher.BeginInvoke(
				() =>
				{
					App.ViewModel.NewRandomRule();
				});
		}

		// Load data for the ViewModel Items
		private void MainPage_Loaded(object sender, RoutedEventArgs e)
		{
			if (!App.ViewModel.IsDataLoaded)
			{
				App.ViewModel.LoadData();
			}
		}

		private void Button_Tap(object sender, GestureEventArgs e)
		{
			App.ViewModel.NewRandomRule();
		}

		private void Panorama_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			Panorama p = sender as Panorama;

			if (p.SelectedIndex == 0)
			{
				ShakeGesturesHelper.Instance.Active = true;
			}
			else
			{
				ShakeGesturesHelper.Instance.Active = false;
			}

		}
	}
}